package com.javasee.rain.stock.service;

import com.javasee.rain.stock.mapper.BlockMovementMapper;
import com.javasee.rain.stock.model.BlockMovement;
import com.javasee.rain.stock.model.BlockMovementDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 板块异动数据服务类
 * 
 * @author : xinyanfei
 * @date : 2025/10/11
 * Copyright (c) 2025, 京东商城 All Rights Reserved.
 */
@RestController
@RequestMapping("/api/block-movement")
@CrossOrigin(origins = "*") // 允许跨域访问
public class BlockMovementService {

    @Autowired
    private BlockMovementMapper blockMovementMapper;

    /**
     * 获取所有板块异动统计信息
     * 
     * @return 板块异动统计列表
     */
    @GetMapping("/blocks")
    public ResponseEntity<Map<String, Object>> getAllBlockMovements() {
        try {
            System.out.println("查询所有板块异动统计信息");
            
            List<BlockMovement> blockMovementList = blockMovementMapper.selectAllBlockMovements();
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", blockMovementList);

            System.out.println("板块异动统计信息查询成功，记录数: " + blockMovementList.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("查询板块异动统计信息失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }

    /**
     * 根据板块名称查询板块异动统计信息
     * 
     * @param blockName 板块名称
     * @return 板块异动统计信息
     */
    @GetMapping("/block/{blockName}")
    public ResponseEntity<Map<String, Object>> getBlockMovementByName(@PathVariable String blockName) {
        try {
            System.out.println("查询板块异动统计信息，板块名称: " + blockName);
            
            BlockMovement blockMovement = blockMovementMapper.selectBlockMovementByName(blockName);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", blockMovement);

            System.out.println("板块异动统计信息查询成功: " + (blockMovement != null ? "找到数据" : "未找到数据"));
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("查询板块异动统计信息失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }

    /**
     * 根据板块名称和股票代码查询该股票的异动信息
     * 
     * @param blockName 板块名称
     * @param stockCode 股票代码
     * @return 异动信息列表
     */
    @GetMapping("/stock-movement")
    public ResponseEntity<Map<String, Object>> getStockMovementByBlockAndCode(
            @RequestParam String blockName, 
            @RequestParam String stockCode) {
        try {
            System.out.println("查询股票异动信息，板块: " + blockName + ", 代码: " + stockCode);
            
            List<BlockMovementDetail> movementList = blockMovementMapper.selectStockMovementByBlockAndCode(blockName, stockCode);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", movementList);

            System.out.println("股票异动信息查询成功，记录数: " + movementList.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("查询股票异动信息失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }

    /**
     * 根据板块名称和日期查询该板块在该日期的异动股票列表
     * 
     * @param blockName 板块名称
     * @param date 日期
     * @return 异动股票列表
     */
    @GetMapping("/stock-list")
    public ResponseEntity<Map<String, Object>> getStockListByBlockAndDate(
            @RequestParam String blockName, 
            @RequestParam String date) {
        try {
            System.out.println("查询板块异动股票列表，板块: " + blockName + ", 日期: " + date);
            
            List<BlockMovementDetail> stockList = blockMovementMapper.selectStockListByBlockAndDate(blockName, date);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", stockList);

            System.out.println("板块异动股票列表查询成功，记录数: " + stockList.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("查询板块异动股票列表失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }

    /**
     * 根据关键字搜索板块异动统计信息
     * 
     * @param keyword 搜索关键字
     * @return 板块异动统计列表
     */
    @GetMapping("/search")
    public ResponseEntity<Map<String, Object>> searchBlockMovements(@RequestParam String keyword) {
        try {
            if (StringUtils.isEmpty(keyword)) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 400);
                response.put("message", "搜索关键字不能为空");
                response.put("data", null);
                return ResponseEntity.badRequest().body(response);
            }

            System.out.println("搜索板块异动统计信息，关键字: " + keyword);
            
            List<BlockMovement> blockMovementList = blockMovementMapper.selectBlockMovementsByKeyword(keyword.trim());
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "成功");
            response.put("data", blockMovementList);

            System.out.println("板块异动统计信息搜索成功，记录数: " + blockMovementList.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("搜索板块异动统计信息失败: " + e.getMessage());
            e.printStackTrace();
            
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", "服务器错误: " + e.getMessage());
            errorResponse.put("data", null);

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(errorResponse);
        }
    }
}